package org.forever.view
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayList;
	import mx.core.UIComponent;
	import mx.logging.ILogger;
	import mx.logging.Log;
	
	import org.forever.util.List;
	import org.forever.util.List_DLNode;
	import org.forever.util.Sequence;
	import org.forever.util.Sequence_DLNode;
	
	import spark.components.BorderContainer;

	
	/**
	 * 折线的实现(初始化状态是3点两边)
	 */
	public class BrokenLine extends UIComponent
	{
		
		private static var _log:ILogger = Log.getLogger("org.forever.view.BrokenLine");
		private var _fromPoint:Point;
		private var _toPoint:Point;
		private var _firstRect:BrokenLineRect;
		private var _secondRect:BrokenLineRect;
		private var _thirdRect:BrokenLineRect;
		private var _firstEdge:BrokenLineEdge;
		private var _secondEdge:BrokenLineEdge;
		private var _rect_w:Number = 6; 
		
		private var _startMove:Boolean = false;
		private var _moving:Boolean = false;
		private var _currentClickRect:BrokenLineRect;
		
		
		private var _rectList:ArrayList;
		private var _edgeList:ArrayList;
		
		
		public function BrokenLine()
		{
		}
		
		/**画折线的方法*/
		public function draw():void{
			
			_firstRect = new BrokenLineRect();
			_secondRect = new BrokenLineRect();
			_thirdRect = new BrokenLineRect();
			_firstEdge = new BrokenLineEdge();
			_secondEdge = new BrokenLineEdge();
			
			initFirstRect();
			initSecondRect();
			initThirdRect();
			initFirstEdge();
			initSecondEdge();
			
			//建立矩形关联边
			_firstRect.secondEdge = _firstEdge;
			_secondRect.firstEdge = _secondEdge;
			_thirdRect.firstEdge = _firstEdge;
			_thirdRect.secondEdge = _secondEdge;
			
			//建立边关联矩形
			_firstEdge.firstRect = _firstRect;
			_firstEdge.secondRect = _thirdRect;
			_secondEdge.firstRect = _thirdRect;
			_secondEdge.secondRect = _secondRect;
			
			//添加顺序导致组件的重叠z轴顺序
			addChild(_firstEdge);
			addChild(_secondEdge);
			addChild(_firstRect);
			addChild(_thirdRect);
			addChild(_secondRect);
			addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			_rectList = new ArrayList(new Array(_firstRect,_thirdRect,_secondRect));
			_edgeList = new ArrayList(new Array(_firstEdge,_secondEdge));
			
		}
		
		public function mouseMoveHandler(event:MouseEvent):void{
			if(_moving){
				_log.debug("mouseMoveHandler.......");
				_currentClickRect.updateFirstEdgePosition();
				_currentClickRect.updateSecondEdgePosition();
			}
		}
		
		public function mouseUpHandler(event:MouseEvent):void{
			if(_moving && null != _currentClickRect){
				_log.debug("mouseUpHandler.......");
				_currentClickRect.updateFirstEdgePosition();
				_currentClickRect.updateSecondEdgePosition();
			}
		}
		
		public function addRectEvent(rect:BrokenLineRect):void{
			rect.addEventListener(MouseEvent.MOUSE_DOWN,rectMouseDownHandler);
			rect.addEventListener(MouseEvent.MOUSE_MOVE,rectMouseMoveHandler);
			rect.addEventListener(MouseEvent.MOUSE_UP,rectMouseUpHandler);
		}
		
		public function rectMouseDownHandler(event:MouseEvent):void{
			_startMove = true;//点击组件做移动准备
			_moving = false;//并未真正移动 
			_currentClickRect = event.currentTarget as BrokenLineRect;
			_currentClickRect.startDrag();
			_log.debug("BrokenLineRect准备开始移动......");
		}
		
		public function rectMouseMoveHandler(event:MouseEvent):void{
			if(_startMove && !_moving){
				_moving = true;
				_log.debug("BrokenLineRect移动中......");
			}
		}	
		
		public function rectMouseUpHandler(event:MouseEvent):void{
			_startMove = false;
			_moving = false;
			_currentClickRect.stopDrag();
			_log.debug("BrokenLineRect停止移动........");
			_currentClickRect.updateFirstEdgePosition();
			_currentClickRect.updateSecondEdgePosition();
		}
		
		
		/**初始化中点到第二个端点的边*/
		public function initSecondEdge():void{
			_secondEdge.firstRect = _thirdRect;
			_secondEdge.secondRect = _secondRect;
			_secondEdge.drawLine();
		}
		
		
		/**初始化第一个端点到中点的边*/
		public function initFirstEdge():void{
			_firstEdge.firstRect = _firstRect;
			_firstEdge.secondRect = _thirdRect;
			_firstEdge.drawLine();
		}
		
		
		/**初始化中点矩形*/
		public function initThirdRect():void{
			_thirdRect.width = _rect_w;
			_thirdRect.height = _rect_w;
			_thirdRect.visible = true;
			_thirdRect.x = (_firstRect.x + _secondRect.x)/2;
			_thirdRect.y = (_firstRect.y + _secondRect.y)/2;
			_thirdRect.setStyle("borderColor","#70B2EE");
			addRectEvent(_thirdRect);
		}
		
		
		/**初始化第二个端点的矩形*/
		public function initSecondRect():void{
			_secondRect.width = _rect_w;
			_secondRect.height = _rect_w;
			_secondRect.visible = true;
			_secondRect.x = _toPoint.x;
			_secondRect.y = _toPoint.y - _secondRect.height/2;
			_secondRect.setStyle("borderColor","#70B2EE");
			addRectEvent(_secondRect);
		}
		
		
		/**初始化第一个端点的矩形*/
		public function initFirstRect():void{
			_firstRect.width = _rect_w;
			_firstRect.height = _rect_w;
			_firstRect.visible = true;
			_firstRect.x = _fromPoint.x - _firstRect.width;
			_firstRect.y = _fromPoint.y - _firstRect.height/2;
			_firstRect.setStyle("borderColor","#70B2EE");
			addRectEvent(_firstRect);
		}
		
		/******************* get and set ******************/
		
		/**线终点*/
		public function get toPoint():Point
		{
			return _toPoint;
		}

		/**
		 * @private
		 */
		public function set toPoint(value:Point):void
		{
			_toPoint = value;
		}

		/**线起点*/
		public function get fromPoint():Point
		{
			return _fromPoint;
		}

		/**
		 * @private
		 */
		public function set fromPoint(value:Point):void
		{
			_fromPoint = value;
		}
		
	}
}
